﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using CNVP.Config;
using CNVP.Framework.Utils;
using System.Text.RegularExpressions;

namespace CNVP.JShop.Publish
{
    public class General
    {
        /// <summary>
        /// 栏目序号
        /// </summary>
        public static int ColumnID { get; set; }
        /// <summary>
        /// 模板类型
        /// </summary>
        public static int TemplateType { get; set; }
        /// <summary>
        /// 文章序号
        /// </summary>
        public static int NewsID { get; set; }
        /// <summary>
        /// 安装路径
        /// </summary>
        private static string InstallDir = UIConfig.InstallDir;
        /// <summary>
        /// 当前使用
        /// </summary>
        private static string TemplateSet = UIConfig.TemplateUse;
        /// <summary>
        /// 默认模板
        /// </summary>
        private static string TempletDir = UIConfig.TemplateDir;
        /// <summary>
        /// 软件版权
        /// </summary>
        private static string CopyRight = "\r\n<!--Publish By JShop(捷点科技) " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "-->";
        /// <summary>
        /// 写入文件
        /// </summary>
        /// <param name="Content">文件内容</param>
        /// <param name="FilePath">物理路径</param>
        public static void WriteHtml(string Content, string FilePath)
        {
            string _FilePath = FilePath;
            int _Length = _FilePath.Length;
            int _IndexOf = FilePath.LastIndexOf('\\');
            if (_IndexOf > 0)
            {
                string _FolderPath = FilePath.Remove(_IndexOf, _Length - _IndexOf);
                if (!FileUtils.FolderExists(_FolderPath))
                {
                    FileUtils.CreateFolder(_FolderPath);
                }
            }
            FileUtils.SaveFile(Content + CopyRight, FilePath);
        }
        #region "读取文件"
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="ColumnID">栏目序号</param>
        /// <param name="TemplateType">0-首页，1-列表，2-内页</param>
        /// <returns></returns>
        public static string ReadHtml(int ColumnID,int TemplateType)
        {
            string strTemplate = string.Empty;
            CNVP.JShop.BLL.CNVP_Template bll = new CNVP.JShop.BLL.CNVP_Template();
            DataTable Dt = bll.GetTemplateInfo(ColumnID, TemplateType,TempletDir);
            if (Dt != null && Dt.Rows.Count > 0)
            {
                string TemplateUrl = Dt.Rows[0]["TemplateUrl"].ToString();
                if (FileUtils.FileExists(HttpContext.Current.Server.MapPath(UIConfig.InstallDir + TemplateUrl)))
                {
                    strTemplate = FileUtils.ReadFile(HttpContext.Current.Server.MapPath(UIConfig.InstallDir + TemplateUrl));
                }
            }
            #region "循环读取"
            if (string.IsNullOrEmpty(strTemplate) && (ColumnID>0))
            {
                int _ColumnType = -1,_ParentID=-1;
                CNVP.JShop.BLL.CNVP_Column bll_Column = new CNVP.JShop.BLL.CNVP_Column();
                DataTable Dt_Column = bll_Column.GetColumnInfo(ColumnID);
                if (Dt_Column != null && Dt_Column.Rows.Count > 0)
                {
                    _ColumnType = Convert.ToInt32(Dt_Column.Rows[0]["ColumnType"]);
                    _ParentID = Convert.ToInt32(Dt_Column.Rows[0]["Depth"]);
                }
                Dt_Column.Dispose();
                Dt_Column.Clear();

                if (_ColumnType == 0 | _ColumnType == 1)
                {
                    if (_ColumnType == 0)
                    {
                        TemplateType = 2;
                    }
                    else
                    {
                        TemplateType = 1;
                    }
                }
                return ReadHtml(_ParentID, TemplateType);
                //if (TemplateType == 0) 
                //{
                //    TemplateType = 1; 
                //}
                //if (Dt_Column != null && Dt_Column.Rows.Count > 0)
                //{
                //   return ReadHtml(Convert.ToInt32(Dt_Column.Rows[0]["ParentID"]), TemplateType);
                //}
                //Dt_Column.Dispose();
                //Dt_Column.Clear();
            }
            #endregion
            return strTemplate;
        }
        #endregion
    }
}